<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form :model="where" class="ele-form-search" label-width="77px" @keyup.enter.native="reload" @submit.native.prevent>
        <el-row :gutter="15">
          <el-col :lg="6" :md="12">
            <el-form-item label="类型名称:">
              <el-input v-model="where.name" clearable placeholder="请输入类型名称" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button v-if="permission.includes('sys:types:index')" class="ele-btn-icon" icon="el-icon-search" type="primary" @click="reload">
                查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table ref="table" :columns="columns" :datasource="url" :selection.sync="selection" :where="where" height="calc(100vh - 315px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button v-if="permission.includes('sys:types:add')" class="ele-btn-icon" icon="el-icon-plus" size="small" type="primary" @click="openEdit(null)">
            添加
          </el-button>
        </template>
        <!-- 站点图片 -->
        <template slot="image_url" slot-scope="{row}">
          <CustomImage :src="row.image"/>
        </template>
        <template slot="warranty_start_unit" slot-scope="{row}">
          <span>{{ row.warranty_start_unit === "year" ? "按年" : "按月" }}</span>
        </template>
        <template slot="warranty_start_type" slot-scope="{row}">
          <span v-if="row.warranty_start_type === 'date_buy'">购买日期</span>
          <span v-else-if="row.warranty_start_type === 'date_check'">验收日期</span>
          <span v-else-if="row.warranty_start_type === 'after_six'">购买后6个月</span>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link v-if="permission.includes('sys:types:edit')" :underline="false" icon="el-icon-edit" type="primary" @click="openEdit(row)">
            修改
          </el-link>
          <el-popconfirm class="ele-action" title="确定要删除此产品类型吗？" @confirm="remove(row)">
            <el-link v-show="permission.includes('sys:types:delete')" slot="reference" :underline="false" icon="el-icon-delete" type="danger">
              删除
            </el-link>
          </el-popconfirm>
          <el-link v-if="permission.includes('sys:product:associateContent')" :underline="false" icon="el-icon-link" type="primary"
                   @click="associateContent(row)">
            关联知识库
          </el-link>
          <el-link v-if="permission.includes('sys:product:associateAttachment')" :underline="false" icon="el-icon-link" type="primary"
                   @click="associateAttachment(row)">
            关联配件
          </el-link>
        </template>
      </ele-pro-table>
    </el-card>
    <associateContent ref="associateContent" @reload="reload"></associateContent>
    <associateAttachment ref="associateAttachment" @reload="reload"></associateAttachment>
    <edit-type ref="TypeEdit" :data="current" :visible.sync="showEdit" @addType="addType" @editType="editType" />
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import EditType from "./type-edit.vue"
import CustomImage from "@/components/CustomImage.vue";
import associateContent from "@/views/product/types/components/associateContent.vue";
import associateAttachment from "@/views/product/types/components/associateAttachment.vue";

export default {
  name: 'SystemLevel',
  components: {
    EditType,
    CustomImage,
    associateContent,
    associateAttachment
  },
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: '/product_types',
      // 表格列配置
      columns: [
        // {
        //   columnKey: 'selection',
        //   type: 'selection',
        //   width: 45,
        //   align: 'center',
        //   fixed: "left"
        // },
        {
          prop: 'type_number',
          label: '编号',
          width: 150,
          align: 'center',
          showOverflowTooltip: true,
          fixed: "left"
        },
        {
          prop: 'name',
          label: '设备型号',
          showOverflowTooltip: true,
          minWidth: 150,
          align: 'center',
        },
        {
          prop: 'category_name',
          label: '分类',
          showOverflowTooltip: true,
          minWidth: 150,
          align: 'center',
        },
        {
          label: '照片',
          showOverflowTooltip: true,
          minWidth: 150,
          align: 'center',
          slot: 'image_url'
        },
        {
          label: '质保开始类型',
          showOverflowTooltip: true,
          minWidth: 150,
          align: 'center',
          slot: 'warranty_start_type'
        },
        {
          prop: 'warranty_cycle_count',
          label: '质保周期时长',
          align: 'center',
          showOverflowTooltip: true,
          width: 150
        },
        {
          label: '质保周期单位',
          showOverflowTooltip: true,
          minWidth: 150,
          align: 'center',
          slot: 'warranty_start_unit'
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 350,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: "right"
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 是否显示导入弹窗
      showImport: false
    };
  },
  methods: {
    // 添加
    addType(value) {
      this.$http.post('product_types', value).then(res => {
        this.$refs.TypeEdit.loading = false;
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        }else {
          this.$message.error(res.data.msg);
        }
        this.$refs.TypeEdit.$refs.form.resetFields()
        this.$refs.TypeEdit.category_name=''
        this.$refs.TypeEdit.updateVisible(false);
      }).catch(e => {
        this.$refs.TypeEdit.loading = false;
        this.$message.error(e.message);
      });
    },
    // 编辑
    editType(value, id) {
      this.$http.put('product_types/' + id, value).then(res => {
        this.$refs.TypeEdit.loading = false;
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        }else {
          this.$message.error(res.data.msg);
        }
        this.$refs.TypeEdit.$refs.form.resetFields()
        this.$refs.TypeEdit.category_name=''
        this.$refs.TypeEdit.updateVisible(false);
      }).catch(e => {
        this.$refs.TypeEdit.loading = false;
        this.$message.error(e.message);
      });
    },
    /*关联知识库*/
    associateContent(row){
      this.$refs.associateContent.showDialog(row)
    },
    /*关联配件*/
    associateAttachment(row){
      this.$refs.associateAttachment.showDialog(row)
    },
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({ where: this.where });
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({ lock: true });
      this.$http.delete('product_types/' + row.id).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    }
  }
}
</script>

<style scoped>
</style>
